package seleniumdemo.web.impl;


import lombok.SneakyThrows;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import seleniumdemo.common.driver.DriverBase;
import seleniumdemo.common.driver.Wait;
import seleniumdemo.web.TestEIPLoginWeb;

import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;


/**
 * @author guoying
 * @version 1.0
 * @date 2021/8/1 15:45
 */

public class TestEIPLoginWebImpl implements TestEIPLoginWeb {




    @SneakyThrows
    @Override
    public void login(String mail,Wait wait,String password) {
        DriverBase driverBase=wait.getDriverBase();
        //获取当前页面标题
        String title=driverBase.getDriver().getTitle();
        //实例化一个显式等待(等待元素出现,超时时间5秒)
        Wait driverWait=new Wait(5,"driverWait",driverBase);
        //实例化一个鼠标键盘操作
        Actions action=new Actions(driverBase.getDriver()) ;

        System.out.println(title);
        if(("科力普管理后台-欢迎登录").equals(title)){
            //获取元素,使用邮箱登录
            By by=By.xpath("/html/body/div/div[2]/div/div[1]/div[2]/div[2]/i");
            //判断元素是否可以点击
            driverWait.driverWaitClick(by);
            //点击邮箱登录
            driverBase.findElement(by).click();
            //判断元素是否可以点击
            by=By.xpath("//*[@id=\"Email\"]");
            driverWait.driverWaitClick(by);
            //清空邮箱和验证码输入框的内容
            driverBase.findElement(by).clear();
            driverBase.findElement(By.xpath("//*[@id=\"mailLogin\"]/div[3]/input")).clear();
            //在输入框中输入邮箱
            driverBase.findElement(by).sendKeys(mail);
            //点击获取验证码
            driverBase.findElement(By.xpath("//*[@id=\"btnVerifyCode\"]")).click();
            //去邮箱获取验证码
            String code=getCode(wait,password,mail);
            if("".equals(code)){
                System.out.println("企业微信邮箱获取验证码失败!");
                return;
            }
            //切换回科力普登录页面
            Set<String> windowHandles = driverBase.getDriver().getWindowHandles();

            String item=windowHandles.stream().filter((row)->{
               driverBase.getDriver().switchTo().window(row);
                String tit=driverBase.getTitle();
                System.out.println(tit);
                return  "科力普管理后台-欢迎登录".equals(tit);

            }).findFirst().get();


            //点击邮箱登录
            driverBase.findElement(by).click();

            //输入验证码
            WebElement codeInput=driverBase.findElement(By.xpath("//*[@id=\"mailLogin\"]/div[3]/input"));
            codeInput.clear();
            codeInput.sendKeys(code);

            //点击登录
            driverBase.findElement(By.xpath("//*[@id=\"btnMailLogin\"]")).click();
            //获取报错信息
            try{
                String result=driverBase.findElement(By.xpath("//*[@id=\"VerifyCode-error\"]")).getText();
                System.out.println("科力普EIP平台登录失败:"+result);
                login(mail, wait, password);
            } catch (Exception e) {
                System.out.println("科力普协调平台登录成功!");
            }

        }else{
            return;
        }
    }


    //打开企业邮箱，获取验证码
    public String getCode(Wait wait, String password, String mail) throws InterruptedException {
        DriverBase driverBase=wait.getDriverBase();
        //打开邮箱
        driverBase.operNewWindows("https://exmail.qq.com/login");
        //切换到新窗口
        Set<String> windowHandles = driverBase.getDriver().getWindowHandles();
        driverBase.getDriver().switchTo().window(windowHandles.toArray()[1].toString());


        //等待元素出现
        try {
            WebElement element = wait.driverWaitClick(By.xpath("//*[@id=\"loginForm\"]/div[3]/div[3]/a[1]"));
            element.click();
        } catch (Exception e) {
            System.out.println("邮箱不能登录");
            return "";
        }
        //输入邮箱账号密码
        WebElement mailElement=driverBase.findElement(By.xpath("//input[@class=\"login_scan_panel_pwd_input\"]"));
        mailElement.clear();
        mailElement.sendKeys(mail);
        WebElement passElement=driverBase.findElement(By.xpath("//*[@id=\"pp\"]"));
        passElement.clear();
        passElement.sendKeys(password);
        //点击登录（需扫过码5天内不用扫码的）
        driverBase.findElement(By.xpath("//*[@id=\"btlogin\"]")).click();
        //查看是否需要扫码
        try{
            WebElement element = wait.visibility(By.xpath("//*[@class=\"wx_login_qrcode_wrap\"]/div[@class=\"title\"]"));
            //打钩5天内新人本机
            driverBase.findElement(By.xpath("//input[@id=\"force_wx_scan_login_tc\"]")).click();
            System.out.println(element.getText());
            System.out.println("企业微信邮箱登录需要扫码,请扫码!!");
            Thread.sleep(20000);
        } catch (Exception e) {
            System.out.println("企业微信邮箱登录成功!");
        }
        //等待收件箱出现
        try {
            WebElement element = wait.driverWaitClick(By.xpath("//*[@id=\"navBarTd\"]/li[@class=\"checkpart fs\"]"));

            element.click();
        } catch (Exception e) {
            System.out.println("打开企业微信邮箱失败!");
            return "";
        }
        Thread.sleep(10000);
        //切换到收件箱的iframe
        driverBase.getDriver().switchTo().frame(driverBase.findElement(By.xpath("//*[@id=\"mainFrame\"]")));
        //点击第一条邮件
        driverBase.findElement(By.xpath("//*[@id=\"frm\"]/div[2]/table[1]/tbody/tr/td[4]")).click();
        driverBase.getDriver().switchTo().defaultContent();
        //进入邮件内容的iframe
        driverBase.getDriver().switchTo().frame(driverBase.findElement(By.xpath("//*[@id=\"mainFrame\"]")));
        //获取邮件内容
        String code=driverBase.findElement(By.xpath("//*[@id=\"mailContentContainer\"]/div/p[3]/span")).getText();
        System.out.println(code);
        return code;

    }
}
